Skip to content

BUG: pivot_table always returns a DataFrame #13554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

yui-knk
Copy link
Contributor

@yui-knk yui-knk commented Jul 3, 2016

Before this commit, if

  • values is not list like
  • columns is None
  • aggfunc is not instance of list

pivot_table returns a Series.

This commit adds checking for columns.nlevels is
greater than 1 to prevent from casting table to
a Series.

This will fix #4386.

'col2': ['C', 'D', 'E'],
'col3': [1, 3, 9]})

pivoted_1 = df.pivot_table('col1', index=['col3', 'col2'], aggfunc=np.sum)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you replicate all 3 tests form the issue:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call this result

index=Index([1, 3, 9], name='col3'),
columns=m)

tm.assert_frame_equal(result, expected)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice case 3 differs from the issue.
Should I rewrite to match with the issue?

The issue:

      sum
col3     
1       3
3       4
9       5
<class 'pandas.core.frame.DataFrame'>

This PR:

      sum
     col1
col3
1       3
3       4
9       5
<class 'pandas.core.frame.DataFrame'>

@jreback jreback changed the title BUG: pivot_table always returns a DataFrame BUG: pivot_table always returns a DataFrame Jul 5, 2016
@jreback
Copy link
Contributor

jreback commented Sep 9, 2016

can you rebase / update?

@jreback
Copy link
Contributor

jreback commented Nov 16, 2016

can you rebase and add a release note for 0.20.0

@yui-knk
Copy link
Contributor Author

yui-knk commented Nov 18, 2016

Sorry to my late response, I rebased and add note for 0.20.0.

@jreback
Copy link
Contributor

jreback commented Dec 26, 2016

can you rebase

@yui-knk
Copy link
Contributor Author

yui-knk commented Dec 27, 2016

Rebased !

@TomAugspurger
Copy link
Contributor

@yui-knk looks like some test failures: https://travis-ci.org/pandas-dev/pandas/jobs/186874138#L1939

Can you test those out locally and see if you reproduce?

@@ -321,3 +321,4 @@ Bug Fixes
- Require at least 0.23 version of cython to avoid problems with character encodings (:issue:`14699`)
- Bug in converting object elements of array-like objects to unsigned 64-bit integers (:issue:`4471`)
- Bug in ``pd.pivot_table()`` where no error was raised when values argument was not in the columns (:issue:`14938`)
- Bug in ``pivot_table`` returns ``Series`` in specific circumstance (:issue:`4386`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add, when it should always return a DataFrame.

@jreback
Copy link
Contributor

jreback commented Dec 30, 2016

can you rebase. I think you have a failing test as well.

@jreback
Copy link
Contributor

jreback commented Feb 27, 2017

can you rebase/update

Before this commit, if

* `values` is not list like
* `columns` is `None`
* `aggfunc` is not instance of `list`

`pivot_table` returns a `Series`.

This commit adds checking for `columns.nlevels` is
greater than 1 to prevent from casting `table` to
a `Series`.

This will fix pandas-dev#4386.
@jreback
Copy link
Contributor

jreback commented Mar 7, 2017

I think you need to change this test to return DataFrame rather than Series. otherwise looks ok: https://travis-ci.org/pandas-dev/pandas/jobs/208224356

@jreback
Copy link
Contributor

jreback commented Mar 7, 2017

also, I'd like to have a sub-section in the whatsnew which shows the case where this used to return a Series, but now returns a DataFrame (you can use one of your tests examples).

@jreback jreback added this to the 0.20.0 milestone Apr 18, 2017
jreback pushed a commit that referenced this pull request Apr 18, 2017
Before this commit, if

* `values` is not list like
* `columns` is `None`
* `aggfunc` is not instance of `list`

`pivot_table` returns a `Series`.

This commit adds checking for `columns.nlevels` is
greater than 1 to prevent from casting `table` to
a `Series`.

This will fix #4386.

DOC: add docs for #13554
@jreback
Copy link
Contributor

jreback commented Apr 18, 2017

thanks @yui-knk rebase and merged.

@jreback jreback closed this Apr 18, 2017
jreback added a commit to jreback/pandas that referenced this pull request Apr 19, 2017
jreback added a commit to jreback/pandas that referenced this pull request Apr 19, 2017
jreback added a commit that referenced this pull request Apr 19, 2017
analyticalmonk pushed a commit to analyticalmonk/pandas that referenced this pull request Apr 20, 2017
Before this commit, if

* `values` is not list like
* `columns` is `None`
* `aggfunc` is not instance of `list`

`pivot_table` returns a `Series`.

This commit adds checking for `columns.nlevels` is
greater than 1 to prevent from casting `table` to
a `Series`.

This will fix pandas-dev#4386.

DOC: add docs for pandas-dev#13554
analyticalmonk pushed a commit to analyticalmonk/pandas that referenced this pull request Apr 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DOC/BUG: pivot_table returns Series in specific circumstance
3 participants